package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.EmsGlycemicIndexBean;
import org.dtrd.modules.food.entity.bean.FoodDetail;
import org.dtrd.modules.food.entity.bean.FoodListInfo;
import org.dtrd.modules.food.entity.request.FoodPageRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 食材库 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-08
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class FoodController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwFoodsService foodsService;

    @ApiOperation(value = "根据升糖指数查询食物列表", response = EmsGlycemicIndexBean.class)
    @PostMapping("1001610")
    @AutoLog(value="根据升糖指数查询食物列表", operateType = OPERATE_TYPE_QUERY)
    public Result<IPage<EmsGlycemicIndexBean>> getApi(@RequestBody FoodPageRequest request) {
        IPage<EmsGlycemicIndexBean> page = foodsService.selectGiFoodList(request.getGiIndex(), request.getFoodName(), request.getPageNum(), request.getPageSize());
        return Result.OK(page);
    }

    @ApiOperation(value = "根据食材ID获取详情", response = FoodDetail.class)
    @GetMapping("1001606")
    @AutoLog(value="根据食材ID获取详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoods(@RequestParam Integer id){
        FoodDetail info = foodsService.getFoods(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询食材库列表", response = FoodListInfo.class)
    @PostMapping("1001605")
    @AutoLog(value="查询食材库列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodsList(@RequestBody FoodPageRequest request){
        handlePageRequest(request);
        IPage<FoodListInfo> page = foodsService.pageFood(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改食材库")
    @PostMapping("1001607")
    @AutoLog(value="新增或修改食材库", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateFoods(@RequestBody FoodDetail dto){
        if (StrUtil.isBlank(dto.getFoodName())
                || dto.getFoodCategoryid() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = foodsService.saveOrUpdateFoods(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除食材")
    @DeleteMapping("1001608")
    @AutoLog(value="删除食材", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeFoods(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = foodsService.removeFoods(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
